﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using AG.Data;

namespace AGMapEditor.Controls
{
    public partial class AGEMapPanel
    {
        #region pos tool method
        private PointF MapPosToUIPoint(int row, int col)
        {
            PointF point = MapPosToMapPoint(row, col);
            point.Y += _originOffsetY;
            return point;
        }

        public Point UIPointToMapPos(PointF uiPoint)
        {
            PointF mapPoint = new PointF(uiPoint.X, uiPoint.Y - _originOffsetY);
            return MapPointToMapPos(mapPoint);
        }

        private PointF MapPosToMapPoint(int row, int col)
        {
            float x = TW / 2 * (row + col);
            float y = TH / 2 * (row - col);
            return new PointF(x, y);
        }

        private Point MapPointToMapPos(PointF point)
        {
            point.Y -= _originOffsetY;
            int col = (int)(point.X / TW - point.Y / TH);
            int row = (int)(point.X / TW + point.Y / TH);
            return new Point(row, col);
        }

        #endregion

        private void SetTerrain(AGMapTile tile)
        {
            if (CurrentTerrain == null || CurrentTerrainTile == null)
            {
                return;
            }

            tile.TerrainId = CurrentTerrain.Id;
            tile.TileId = CurrentTerrainTile.Id;
            tile.TileIndex = (byte)(new Random().Next(0, CurrentTerrainTile.Count));
        }

        /// <summary>
        /// 设置装饰物
        /// </summary>
        /// <param name="tile"></param>
        private void SetObje2(AGMapTile tile)
        {
            AGMapObject obj1 = new AGMapObject();
            obj1.Id = _map.GetObjId();
            obj1.Caption = string.Format("object-{0}", obj1.Id);
            obj1.PlayerId = 0;
            obj1.UnitId = CurUnit.Id;
            obj1.PosRow = tile.PosRow;
            obj1.PosCol = tile.PosCol;
            _map.ObjectList.Add(obj1);
            UpdateObjectList();
        }

        private void UpdateObjectList()
        {
            for (int index = 0; index < _observerList.Count; index++)
            {
                _observerList[index].UpdateObjectList();
            }
        }

        private void UpdateSwitchPointList()
        {
            for (int index = 0; index < _observerList.Count; index++)
            {
                _observerList[index].UpdateSwitchPointList();
            }
        }

        private void SetBlock(AGMapTile tile)
        {
            tile.IsBlock = !tile.IsBlock;
        }
    }
}
